package com.cmd.project.controller;

import cn.hutool.core.convert.Convert;
import com.cmd.common.annotation.Anonymous;
import com.cmd.common.annotation.Log;
import com.cmd.common.core.controller.BaseController;
import com.cmd.common.core.domain.AjaxResult;
import com.cmd.common.core.page.TableDataInfo;
import com.cmd.common.enums.BusinessType;
import com.cmd.common.utils.poi.ExcelUtil;
import com.cmd.project.domain.ProActivities;
import com.cmd.project.service.IProActivitiesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 赛事活动Controller
 *
 * @author cq
 * @date 2025-04-03
 */
@RestController
@RequestMapping("/project/activities")
public class ProActivitiesController extends BaseController {
    @Autowired
    private IProActivitiesService proActivitiesService;

    /**
     * 查询赛事活动列表
     */
    @PreAuthorize("@ss.hasPermi('project:activities:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProActivities proActivities) {
        startPage();
        List<ProActivities> list = proActivitiesService.selectProActivitiesList(proActivities);
        return getDataTable(list);
    }

    /**
     * 导出赛事活动列表
     */
    @PreAuthorize("@ss.hasPermi('project:activities:export')")
    @Log(title = "赛事活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProActivities proActivities) {
        List<ProActivities> list = proActivitiesService.selectProActivitiesList(proActivities);
        ExcelUtil<ProActivities> util = new ExcelUtil<ProActivities>(ProActivities.class);
        util.exportExcel(response, list, "赛事活动数据");
    }

    /**
     * 获取赛事活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('project:activities:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(proActivitiesService.selectProActivitiesById(id));
    }

    /**
     * 新增赛事活动
     */
    @PreAuthorize("@ss.hasPermi('project:activities:add')")
    @Log(title = "赛事活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProActivities proActivities) {
        return toAjax(proActivitiesService.insertProActivities(proActivities));
    }

    /**
     * 修改赛事活动
     */
    @PreAuthorize("@ss.hasPermi('project:activities:edit')")
    @Log(title = "赛事活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProActivities proActivities) {
        return toAjax(proActivitiesService.updateProActivities(proActivities));
    }

    /**
     * 删除赛事活动
     */
    @PreAuthorize("@ss.hasPermi('project:activities:remove')")
    @Log(title = "赛事活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(proActivitiesService.deleteProActivitiesByIds(ids));
    }

    @GetMapping("/getProActivity")
    @Anonymous
    public AjaxResult getProActivity() {
        ProActivities vo = proActivitiesService.getProActivity();
        return AjaxResult.success(vo);
    }


    @PostMapping(value = "/sendEmail")
    @Anonymous
    public AjaxResult sendEmail(@RequestBody Map<String, Object> map) {

        proActivitiesService.sendEmail(Convert.toLong(map.get("id")), Convert.toStr(map.get("email")) );
        return success();
    }
}
